local ExternalFun = appdf.req(appdf.EXTERNAL_SRC .. "ExternalFun")


local BindAliPayLayer = class("BindAliPayLayer",function(scene)
    local alipay = display.newLayer(cc.c4b(0, 0, 0, 180))
	return alipay
end)

function BindAliPayLayer:ctor(scene)
    ExternalFun.registerNodeEvent(self)
    self._scene = scene

    self:loadCSB()
    self:init()

	self:setContentSize(yl.WIDTH,yl.HEIGHT)
	
	local events = {"SETALIPAYID",}
   self.tb_listen = appdf.initListener(events,handler(self,self.customEventHandler),self)

end
function BindAliPayLayer:onExit()
	appdf.removeListener(self.tb_listen)
end
function BindAliPayLayer:customEventHandler(event)
	if event.name == "SETALIPAYID" then
		if event.data.result == 1 then
			if self.bandingInfo.type == 1 then	--ali
				showToast(self._scene, "支付宝账号绑定成功!", 2)
				GlobalUserItem.szAliPayAcccount = self.bandingInfo.bindid
				GlobalUserItem.szRealyName = self.bandingInfo.bindname
				
			elseif self.bandingInfo.type == 2 then	--bank
				showToast(self._scene, "银行卡账号绑定成功!", 2)
				GlobalUserItem.bankid = self.bandingInfo.bindid
				GlobalUserItem.bank_username = self.bandingInfo.bindname
				
			end
			self._scene:reSetBindingInfo(self.bandingInfo)

			self:removeFromParent()
		end
	end
end

function BindAliPayLayer:loadCSB()
	local _,root = ExternalFun.loadRootCSB("userinfo/bindAlipayLayer.csb",self)
	ExternalFun.FixTopUI(root)
	self.bg = root:getChildByName("bg")
	ExternalFun.FixScaleUI(self.bg)
    local bg = self.bg
	ExternalFun.playOpenLayerAnimation( self.bg )
    self.btn_close = bg:getChildByName("btn_close")
    self.btn_sure = bg:getChildByName("btn_confirm")
    self.tf_alipaycount = bg:getChildByName("tf_alipaycount")
	self.tf_alipayname = bg:getChildByName("tf_alipayname")
	-- self.tf_alipayname:setMaxLength(30) 
	self.txt_tips = bg:getChildByName("txt_tips")
	self.txt_bankname = bg:getChildByName("tf_bankname")
	self.txt_tf_bankarea = bg:getChildByName("tf_bankarea")

    local btnCallback = function(ref,type)
		if type == ccui.TouchEventType.ended then
			ref:setScale(1)
			ExternalFun.playClickEffect()
			self:onButtonClicked(ref:getTag(),ref)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then
			ref:setScale(1)
		end
    end
    
    self.btn_close:addTouchEventListener(btnCallback)
    self.btn_sure:addTouchEventListener(btnCallback)
end

function BindAliPayLayer:init()
	local size = self.tf_alipaycount:getContentSize()
	local posx,posy = self.tf_alipaycount:getPosition()
	self.tf_alipaycount:removeFromParent()
	self.tf_alipaycount = nil

	self.tf_alipaycount =  ccui.EditBox:create(size,ccui.Scale9Sprite:create(""))
	:setAnchorPoint(cc.p(0.5,0.5))
	:setPosition(cc.p(posx,posy))
	:setFontSize(27)
	:setPlaceholderFontSize(27)
	:setInputMode(cc.EDITBOX_INPUT_MODE_EMAILADDR)
	:setMaxLength(30)
	:setPlaceHolder("输入支付宝账号")
	 self.tf_alipaycount:registerScriptEditBoxHandler(function(event,editbox)
        if event == "return" then
           local edittext = editbox:getText()
			edittext = tostring(edittext)
			if  self:CheckChinese(edittext) then
				showToast(self._scene,"含有非法字符",2)
				editbox:setText("")
				return
			end
			editbox:setText(edittext)
			if string.find(edittext,'[a-zA-Z]')  then
				if not string.find(edittext,'@') or not string.find(edittext,'%.') then
					
					self.nopass = true
				else
					self.nopass = false
				end
			elseif string.find(edittext,'[a-zA-Z0-9]') == nil then
				self.nopass = true
			else
				self.nopass = false
			end
        end
    end)

	size = self.tf_alipayname:getContentSize()
	posx,posy = self.tf_alipayname:getPosition()
	self.tf_alipayname:removeFromParent()		
	self.tf_alipayname =  ccui.EditBox:create(size,ccui.Scale9Sprite:create(""))
	:setAnchorPoint(cc.p(0.5,0.5))
	:setPosition(cc.p(posx,posy))
	:setFontSize(27)
	:setPlaceholderFontSize(27)
	:setMaxLength(30)
	:setPlaceHolder("输入收款人姓名")
	
	
	self.bg:addChild(self.tf_alipaycount)
	self.bg:addChild(self.tf_alipayname)
end
 function BindAliPayLayer:CheckChinese(s) 
    local ret = {};
    local f = '[%z\1-\127\194-\244][\128-\191]*';
    for v in s:gfind(f) do
		local isChinese = (#v~=1)
        if isChinese then
			return true
		end
    end
    return false;
end
-- type: 1 - 支付宝 2 - 银行卡
function BindAliPayLayer:onConfirm()
	local msg = {}
	msg.type = 1
	local bknus = string.rtrim( string.ltrim( self.tf_alipaycount:getText() ))
	local bkname = string.rtrim( string.ltrim(self.tf_alipayname:getText()) )

	if bknus == "" or bkname == "" then
		showToast(self, "请输入正确的绑定信息", 2, cc.c3b(255, 255, 0))
		return 
	end
	
	local filter = string.find(bknus, "^[a-zA-Z0-9@%.]+")
	if nil == filter then
		showToast(self, "帐号包含非法字符, 请重试!", 1)
		return
	end
	msg.bindid = bknus
	msg.bindname = bkname
	self.bandingInfo = msg
	local calback = function ()
		appdf.clientSocket:sendBindMoneyInfo(msg)
	end
	self:showBindTipsLayer(msg,calback)
end

function BindAliPayLayer:showBindTipsLayer(info,cb)
	local csbnode = ExternalFun.loadCSB("userinfo/bindTipsLayer.csb", self)
	local banklayer = csbnode:getChildByName("bank_Panel")
	local ali_Panel = csbnode:getChildByName("ali_Panel")
	banklayer:setVisible(false)
	ExternalFun.playOpenLayerAnimation( ali_Panel )
	ali_Panel:getChildByName("aliaccount"):setString("支付宝账号："..info.bindid)
	ali_Panel:getChildByName("aliname"):setString("收款人姓名："..info.bindname)
	ali_Panel:getChildByName("ok_btn"):addTouchEventListener(function(ref,type)
		if type == ccui.TouchEventType.ended then
			ref:setScale(1)
			ExternalFun.playClickEffect()
			if cb then
				cb()
			end
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then
			ref:setScale(1)
		end
	end)
	ali_Panel:getChildByName("closebtn"):addTouchEventListener(function(ref,type)
		if type == ccui.TouchEventType.ended then
			ref:setScale(1)
			ExternalFun.playClickEffect()
			csbnode:removeFromParent()
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then
			ref:setScale(1)
		end
	end)
end
function BindAliPayLayer:onButtonClicked(tag, ref)
	if ref == self.btn_close then
		self:removeFromParent()
	elseif ref == self.btn_sure then
		if self.nopass then
			showToast(self._scene,"请输入正确的支付宝账号",2)
			return
		end
		self:onConfirm()
	end
end

return BindAliPayLayer